/**
  * @description: 获取当前位置天气
  * @author: name
  * @update: name(xxxx-xx-xx)
  */
function jsonp({ url, data, callbackName = 'callback', success }) {
  window[callbackName] = success
  const s = document.createElement('script')
  s.src = `${url}?callback=${callbackName}&${Object.keys(data).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key])).join('&')}`
  document.body.appendChild(s)
}
export function getWeather(needPosition = true) {
  return new Promise((resolve) => {
    function getWeatherInfo(city) {
      jsonp({
        url: 'https://query.asilu.com/weather/baidu',
        data: {
          city
        },
        success(data) {
          resolve(data)
        }
      })
    }
    if (needPosition) {
      jsonp({
        url: 'https://apis.map.qq.com/ws/location/v1/ip',
        data: {
          key: 'PTMBZ-GCQLW-SC2RG-R2FNI-HWPNQ-4PBQM',
          output: 'jsonp'
        },
        success(res) {
          getWeatherInfo(res.result.ad_info.city)
        }
      })
    } else {
      getWeatherInfo('深圳')
    }
  })
}
